package com.changgou.util;

import com.changgou.file.FastDFSFile;
import org.csource.common.MyException;
import org.csource.common.NameValuePair;
import org.csource.fastdfs.*;
import org.springframework.core.io.ClassPathResource;

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;

/***
 * 文件工具类
 */
public class FastDfsUtil {
    static {
        try {
            //查找文件路径
            String filename=new ClassPathResource("fdfs_client.conf").getPath();
            //读取配置信息
            ClientGlobal.init(filename);
        } catch (IOException e) {
            e.printStackTrace();
        } catch (MyException e) {
            e.printStackTrace();
        }

    }

    /**
     * 文件上传
     * @param fastDFSFile 要上传的文件信息封装体
     */
    public static String[] upload(FastDFSFile fastDFSFile) throws Exception {

        //通过TrackerClient对象获取TrackerServer信息
        TrackerServer trackerServer = getTrackerServer();
        //获取StorageClient对象
        StorageClient storageClient = getStorageClient(trackerServer);
        //执行文件上传
        //获取文件作者
        NameValuePair[] meta_list = new NameValuePair[1];
        meta_list[0] =new NameValuePair("author",fastDFSFile.getAuthor());

        /**
         * 参数一 文件字节数组
         * 参数二 文件后缀
         * 参数三 文件附加信息
         *
         * return 文件存储位置
         *  参数1 组名
         *  参数2 文件名
         */
        String[] strings = storageClient.upload_file(fastDFSFile.getContent(), fastDFSFile.getExt(), meta_list);
        return strings;
    }

    /**
     *
     * @param groupName 文件组名
     * @param remoteFileName    文件仓库信息
     * @return
     */
    public static FileInfo getFile(String groupName, String remoteFileName) throws Exception {

        //通过TrackerClient对象获取TrackerServer信息
        TrackerServer trackerServer = getTrackerServer();
        //获取StorageClient对象
        StorageClient storageClient = getStorageClient(trackerServer);

        return storageClient.get_file_info(groupName,remoteFileName);
    }
    /**
     * 文件下载
     * @param groupName 文件组名
     * @param remoteFileName    文件仓库信息
     * @return
     */
    public static InputStream downloadFile(String groupName, String remoteFileName) throws Exception{
        TrackerServer trackerServer = getTrackerServer();
        StorageClient storageClient = getStorageClient(trackerServer);
        //文件下载
        byte[] buffer = storageClient.download_file(groupName, remoteFileName);
        return new ByteArrayInputStream(buffer);
    }

    /**
     * 删除文件
     * @param groupName
     * @param remoteFileName
     * @throws Exception
     */
    public static void deleteFile(String groupName, String remoteFileName) throws Exception{
        TrackerServer trackerServer = getTrackerServer();
        StorageClient storageClient = getStorageClient(trackerServer);
        //删除文件
        storageClient.delete_file(groupName,remoteFileName);
    }

    /**
     * 获取 storage信息
     * @return
     * @throws Exception
     */
    public static StorageServer getStorages() throws Exception{
        //创建TrackerClient客户端对象
        TrackerClient trackerClient = new TrackerClient();
        TrackerServer trackerServer = getTrackerServer();
        StorageServer storeStorage = trackerClient.getStoreStorage(trackerServer);
        return storeStorage;
    }
    /***
     * 获取Storage信息,IP和端口
     * @param groupName
     * @param remoteFileName
     * @return
     * @throws IOException
     */
    public static ServerInfo[] getFetchStorages(String groupName,
                                                String remoteFileName) throws IOException {
        TrackerClient trackerClient = new TrackerClient();
        TrackerServer trackerServer = trackerClient.getConnection();
        return trackerClient.getFetchStorages(trackerServer, groupName, remoteFileName);
    }

    /***
     * 获取Tracker服务地址
     * @return
     * @throws IOException
     */
    public static String getTrackerUrl() throws Exception {
        return "http://"+getTrackerServer().getInetSocketAddress().getHostString()+":"+ClientGlobal.getG_tracker_http_port()+"/";
    }


    //优化部分,代码抽取

    /**
     * 获取 TrakcrServer
     * @return
     * @throws Exception
     */
    public static TrackerServer getTrackerServer() throws Exception{
        //创建TrackerClient客户端对象
        TrackerClient trackerClient = new TrackerClient();
        //通过TrackerClient对象获取TrackerServer信息
        TrackerServer trackerServer = trackerClient.getConnection();

        return trackerServer;
    }

    /**
     * 获取 StorageClient
     * @param trackerServer
     * @return
     */
    public static StorageClient getStorageClient(TrackerServer trackerServer){
        StorageClient storageClient = new StorageClient(trackerServer,null);
        return storageClient;
    }
}
